package com.zyd.shiro.controller;

import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.User;
import com.zyd.shiro.business.enums.ResponseStatus;
import com.zyd.shiro.business.service.CourseService;
import com.zyd.shiro.business.service.SysUserService;
import com.zyd.shiro.business.vo.CourseConditionVO;
import com.zyd.shiro.framework.object.PageResult;
import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.persistence.beans.Course;
import com.zyd.shiro.util.ResultUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private SysUserService userService;

    @RequiresPermissions("course")
    @PostMapping("/list")
    public PageResult getAll(CourseConditionVO vo) throws Exception {
        Subject subject = SecurityUtils.getSubject();
        Long userId = Long.parseLong(subject.getPrincipal().toString());
        User user = userService.getByPrimaryKey(userId);
        if(ObjectUtils.isEmpty(user)){
            throw new Exception("请登录");
        }
        PageInfo<Course> pageInfo = courseService.findPageBreakByCondition(vo,user);
        return ResultUtil.tablePage(pageInfo);
    }

    @RequiresPermissions("course:add")
    @PostMapping(value = "/add")
    public ResponseVO add(Course course) {
        try {
            courseService.insert(course);
            return ResultUtil.success("成功");
        }catch (Exception e){
            return ResultUtil.error("添加失败:" + e.getMessage());
        }
    }

    @RequiresPermissions(value = {"course:batchDelete", "course:delete"}, logical = Logical.OR)
    @PostMapping(value = "/remove")
    public ResponseVO remove(Long[] ids) {
        if (null == ids) {
            return ResultUtil.error(500, "请至少选择一条记录");
        }
        Subject subject = SecurityUtils.getSubject();
        Long userId = Long.parseLong(subject.getPrincipal().toString());
        for (Long id : ids) {
            courseService.removeCourse(id,userId);
        }
        return ResultUtil.success("成功删除 [" + ids.length + "] 个课程");
    }

    @RequiresPermissions("course:edit")
    @PostMapping("/get/{id}")
    public ResponseVO get(@PathVariable Long id) {
        return ResultUtil.success(null, this.courseService.getByPrimaryKey(id));
    }

    @RequiresPermissions("course:edit")
    @PostMapping("/edit")
    public ResponseVO edit(Course course) {
        try {
            courseService.updateSelective(course);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("课程修改失败！");
        }
        return ResultUtil.success(ResponseStatus.SUCCESS);
    }

    @RequiresPermissions("course:chooseCourse")
    @PostMapping("/chooseCourse")
    public ResponseVO chooseCourse(Long id) {
        try {
            Subject subject = SecurityUtils.getSubject();
            Long userId = Long.parseLong(subject.getPrincipal().toString());
            courseService.chooseCourse(id,userId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("选课失败！");
        }
        return ResultUtil.success(ResponseStatus.SUCCESS);
    }

    @RequiresPermissions("course:chooseCourse")
    @PostMapping("/chooseCancel")
    public ResponseVO chooseCancel(Long id) {
        try {
            Subject subject = SecurityUtils.getSubject();
            Long userId = Long.parseLong(subject.getPrincipal().toString());
            courseService.chooseCancel(id,userId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("取消失败！");
        }
        return ResultUtil.success(ResponseStatus.SUCCESS);
    }
}
